package com.cn.util;

import com.cn.msg.Message;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.sql.SQLOutput;
import java.text.NumberFormat;
import java.util.concurrent.atomic.AtomicInteger;

public class UtilAll {

    final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    private static AtomicInteger opaque = new AtomicInteger(0);

    public static int getRequestId() {
        return opaque.incrementAndGet();
    }

    public static String offset2FileName(long offset) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        //长度20
        numberFormat.setMinimumIntegerDigits(20);// 设置数值的整数部分允许的最大位数
        numberFormat.setMaximumFractionDigits(0);//设置数值的小数部分允许的最大位数
        numberFormat.setGroupingUsed(false);//去掉逗号
        return numberFormat.format(offset);
    }

    public static ByteBuffer encodeHeader(Message msg) {
        ByteBuffer header = ByteBuffer.allocate(11);
        header.limit(11);
        header.put(msg.getType());// 1
        header.put(msg.getFlag());// 1
        header.put(msg.getNettyRequestWrapperCode());// 1
        header.putLong(msg.getOpaque()); // 8
        header.flip();
        return header;
    }

    /**
     * 获取pid
     *
     * @return
     */
    public static int getPid() {
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String name = runtime.getName(); // format: "pid@hostname"
        try {
            return Integer.parseInt(name.substring(0, name.indexOf('@')));
        } catch (Exception e) {
            return -1;
        }
    }

    public static String byte2String(byte[] src) {
        char[] result = new char[src.length * 2];
        for (int i = 0; i < src.length; i++) {
            //byte范围 -128-127
            //转换为 0 - 255之内的数字
            int v = src[i] & 0xFF;
//            System.out.println(v + "=" + src[i]);
            result[i * 2] = HEX_ARRAY[v >>> 4];
            //转换为0-15之内的数字
            result[i * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }

        return new String(result);
    }

    public static byte[] string2bytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static void main(String[] args) {
        String txt = "姚进。家婆的啊.阿来得及罚款来得及发快递就发到看";
        String result = byte2String(txt.getBytes());
        System.out.println(result);
        byte[] b = string2bytes(result);
        System.out.println(new String(b));
    }

}
